package com.comunity_cloud.client.model.upload_file;

import java.io.UnsupportedEncodingException;

import com.comunity_cloud.client.model.communication.packet.PeerPacketHeader;

public class UploadFileHeader extends PeerPacketHeader {

	private long offset;
	
	private long chunkSize;
	
	private long totalSize;
	
	private long totalOffset;
	
	private int primary;
	
	private String fileName;

	private byte[] fileNameBytes;
	
	public UploadFileHeader() {
		this.type = Type.UPLOAD_FILE;
	}

	@Override
	public void convertToBytes() {
		byteBuffer.putLong(offset);
		byteBuffer.putLong(chunkSize);
		byteBuffer.putLong(totalSize);
		byteBuffer.putLong(totalOffset);
		byteBuffer.putInt(primary);
		byteBuffer.put(fileNameBytes);
	}
	
	@Override
	protected void fromBytes() {
		offset 		= byteBuffer.getLong();
		chunkSize 	= byteBuffer.getLong();
		totalSize 	= byteBuffer.getLong();
		totalOffset = byteBuffer.getLong();
		primary 	= byteBuffer.getInt();
		fileName 	= new String(bytes,byteBuffer.position(),byteBuffer.limit() - byteBuffer.position());
	}

	@Override
	protected int headerLength() {
		int length = 0;
		try {
			fileNameBytes = fileName.getBytes("UTF8");
			length = fileNameBytes.length;
		} catch (UnsupportedEncodingException e) {}
		
		return 8 + 8 + 8 + 8 + 4 + length;
	}

	public void setOffset(long offset) {
		this.offset = offset;
	}
	
	public long getOffset() {
		return offset;
	}
	
	public String getFileName() {
		return fileName;
	}

	public long getTotalSize() {
		return totalSize;
	}

	public void setTotalSize(long totalSize) {
		this.totalSize = totalSize;
	}

	public long getTotalOffset() {
		return totalOffset;
	}

	public void setTotalOffset(long totalOffset) {
		this.totalOffset = totalOffset;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	public long getChunkSize() {
		return chunkSize;
	}
	
	public void setChunkSize(long chunkSize) {
		this.chunkSize = chunkSize;
	}

	public int getPrimary() {
		return primary;
	}
	
	public void setPrimary(int primary) {
		this.primary = primary;
	}
	
	@Override
	public String toString() {
		String result = "";
		result+=fileName;
		return result;
	}
}
